﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week03.wenk
{
    public class ScratchesCreateHandler : IRequestHandler<ScratchesCreateCommend, Result>
    {
        private readonly IBaseRepository<Scratches> Scratches;
        private readonly IMapper mapper;

        public ScratchesCreateHandler(IMapper mapper, IBaseRepository<Scratches> Scratches)
        {
            this.mapper = mapper;
            this.Scratches = Scratches;
        }

        public async Task<Result> Handle(ScratchesCreateCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Scratches scratches = mapper.Map<Scratches>(request);
                scratches.ScratchesDate.ToString("yyyy-HH-dd");
                var res = Scratches.Add(scratches);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
